//
//  BBDetailCell.swift
//  BBetter
//
//  Created by Gmc on 2020/3/10.
//  Copyright © 2020 Gmc. All rights reserved.
//

import UIKit
let BBETTER_PUNCHED_NOTIFICATION = "BBetter_punched_notification"
class BBDetailCell: UITableViewCell {
    var model:BBDetailDescModel = BBDetailDescModel() {
        didSet {
            timeLabel.text = model.time
            descLabel.text = model.text
            todayLabel.text = "打卡: \(model.today)"
        }
    }
    fileprivate var timeLabel = UILabel()
    fileprivate var descLabel = UILabel()
    fileprivate var todayLabel = UILabel()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initSubviews() {
        contentView.addSubview(timeLabel)
        contentView.addSubview(descLabel)
        contentView.addSubview(todayLabel)
        
        if (CheckDate.check(date: "04-04") || CheckDate.check(date: "12-13")) {
            todayLabel.textColor = UIColor.colorWithHexString(hexString: "272727")
        } else {
            todayLabel.textColor = UIColor.colorWithHexString(hexString: "00BFFF")
        }

        timeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView).offset(20)
        }
        todayLabel.snp.makeConstraints { (make) in
            make.trailing.equalTo(contentView).offset(-20)
            make.centerY.equalTo(timeLabel)
        }
        descLabel.snp.makeConstraints { (make) in
            make.top.equalTo(timeLabel.snp_bottom).offset(4)
            make.leading.equalTo(timeLabel)
            make.trailing.equalTo(contentView).offset(-20)
        }
    }

}

class BBDetailInfoCell: UITableViewCell {
    var model:BBDetailModel = BBDetailModel() {
        didSet {
            countLabel.text = "总数: \(model.denominator)"
            progressLabel.text = "进度: \(model.numerator)"
            todayLabel.text = "今日: \(model.today)"
            if model.denominator == model.numerator {
                punchCard.setTitle("任务完成", for: .normal)
                punchCard.isEnabled = false
            } else if model.today != 0 {
                punchCard.setTitle("今日已打卡", for: .normal)
                punchCard.isEnabled = false
            } else {
                punchCard.setTitle("今日打卡", for: .normal)
                punchCard.isEnabled = true
            }
        }
    }
    private var punchCard:UIButton = UIButton(type: .custom)
    fileprivate var nameLabel = UILabel()
    fileprivate var clusterLabel = UILabel()
    fileprivate var countLabel = UILabel()
    fileprivate var progressLabel = UILabel()
    fileprivate var todayLabel = UILabel()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        initSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc fileprivate func punchAction(sender: UIButton) {
        let notification = Notification(name: Notification.Name(rawValue: BBETTER_PUNCHED_NOTIFICATION))
        NotificationCenter.default.post(notification)
    }
    
    fileprivate func initSubviews() {
        do {
            countLabel.text = "总数: 0"
            progressLabel.text = "进度: 0"
            todayLabel.text = "今日: 0"
            
            if (CheckDate.check(date: "04-04") || CheckDate.check(date: "12-13")) {
                todayLabel.textColor = UIColor.colorWithHexString(hexString: "272727")
            } else {
                todayLabel.textColor = UIColor.colorWithHexString(hexString: "00BFFF")
            }

            countLabel.textAlignment = .center
            progressLabel.textAlignment = .center
            todayLabel.textAlignment = .center
            contentView.addSubview(nameLabel)
            contentView.addSubview(clusterLabel)
            contentView.addSubview(countLabel)
            contentView.addSubview(progressLabel)
            contentView.addSubview(todayLabel)
            progressLabel.snp.makeConstraints { (make) in
                make.centerX.equalTo(contentView)
                make.top.equalTo(contentView).offset(12)
            }
            countLabel.snp.makeConstraints { (make) in
                make.centerY.equalTo(progressLabel)
                make.trailing.equalTo(progressLabel.snp_leading).offset(-20)
                make.leading.equalTo(contentView).offset(20)
            }
            
            todayLabel.snp.makeConstraints { (make) in
                make.centerY.equalTo(countLabel)
                make.leading.equalTo(progressLabel.snp_trailing).offset(20)
                make.trailing.equalTo(contentView).offset(-20)
            }
        }
        
        do {
            punchCard.setTitle("今日打卡", for: .normal)
            
            if (CheckDate.check(date: "04-04") || CheckDate.check(date: "12-13")) {
                punchCard.setTitleColor(UIColor.colorWithHexString(hexString: "272727"), for: .normal)
            } else {
                punchCard.setTitleColor(UIColor.colorWithHexString(hexString: "00BFFF"), for: .normal)
            }

            punchCard.setTitleColor(UIColor.colorWithHexString(hexString: "DDDDDF"), for: .highlighted)
            punchCard.setTitleColor(UIColor.colorWithHexString(hexString: "DDDDDF"), for: .disabled)
            punchCard.addTarget(self, action: #selector(punchAction(sender:)), for: .touchUpInside)
            contentView.addSubview(punchCard)
            punchCard.snp.makeConstraints { (make) in
                make.top.equalTo(progressLabel.snp_bottom).offset(8)
                make.centerX.equalTo(contentView)
            }
        }
    }
}


class BBDetailDrawCell: UITableViewCell {
    var model:BBDetailModel = BBDetailModel() {
        didSet {
            self.data = model.desc
            collectionView.reloadData()
            let index = IndexPath(item: self.data.count - 1, section: 0)
            collectionView.scrollToItem(at: index, at: .centeredHorizontally, animated: false)
        }
    }
    
    
    private var height:CGFloat = 0.0
    private var width:CGFloat = 0.0
    var collectionView:UICollectionView!
    var data:[BBDetailDescModel] = [BBDetailDescModel]()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        initSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initSubviews() {
        backgroundColor = UIColor.random()
        let flow = UICollectionViewFlowLayout()
        flow.scrollDirection = .horizontal
        flow.itemSize = CGSize(width: 50, height: 200)
        flow.minimumLineSpacing = 0.001
        flow.minimumInteritemSpacing = 0.001
        collectionView = UICollectionView(frame: contentView.bounds, collectionViewLayout: flow)
        collectionView.register(BBDrawCell.self, forCellWithReuseIdentifier: "cellID")
        collectionView.delegate = self
        collectionView.dataSource = self
        if #available(iOS 13.0, *) {
            if traitCollection.userInterfaceStyle == .dark {
                collectionView.backgroundColor = .black
            } else {
                collectionView.backgroundColor = .white
            }
        } else {
            collectionView.backgroundColor = .white
        }
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    }
    
}

extension BBDetailDrawCell: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.data.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! BBDrawCell
        let detail = data[indexPath.item]
        
        cell.model = detail
        let height = CGFloat(detail.today) / CGFloat(model.denominator) * 170
        cell.progressH = height
        return cell
    }
}


